package com.qiu.wxapi.controller;

import com.qiu.wxapi.commom.ResponseResult;
import com.qiu.wxapi.mapper.UserMapper;
import com.qiu.wxapi.pojo.User;
import com.qiu.wxapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 登录
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;


    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PostMapping("update")
    public ResponseResult update(@RequestBody User user) {
        if (user == null) {
            return ResponseResult.fail("不能为空");
        }
        int insert = this.userMapper.updateByPrimaryKey(user);
        user.setPassword(null);
        return ResponseResult.success(user);
    }


    /**
     * 查询用户
     */
    @GetMapping
    public ResponseResult find() {
        Example example = new Example(User.class);
        example.setOrderByClause("status  DESC");
        List<User> userList = this.userMapper.selectByExample(example);
        List<User> users = userList.stream().map(user -> {
            user.setPassword(null);
            return user;
        }).collect(Collectors.toList());
        return ResponseResult.success(users);
    }

    /**
     * 设置管理
     * @param userId
     * @param role
     * @return
     */
    @RequestMapping("setAdmin")
    public ResponseResult setAdmin(@RequestParam("userId") Integer userId, @RequestParam("role") String role) {
        User user = this.userMapper.selectByPrimaryKey(userId);
        user.setRole(role);
        int i = this.userMapper.updateByPrimaryKey(user);
        return ResponseResult.success(i);
    }

    /**
     * 根据id查询用户
     *
     * @param userId
     * @return
     */
    @GetMapping("findOne")
    public ResponseResult findOne(@RequestParam Integer userId) {
        if (userId == null) {
            return ResponseResult.fail("不能为空");
        }
        User user = this.userMapper.selectByPrimaryKey(userId);
        return ResponseResult.success(user);
    }

    /**
     * 删除用户
     *
     * @param user
     * @return
     */
    @PostMapping("delete")
    public ResponseResult delete(@RequestBody User user) {
        if (user == null) {
            return ResponseResult.fail("不能为空");
        }
        int delete = this.userMapper.delete(user);
        return ResponseResult.success(delete);
    }


    /**
     * 普通登录
     *
     * @param user
     * @return
     */
    @PostMapping("doLogin")
    public ResponseEntity<User> doLogin(@RequestBody User user) {
        User user1 = userService.doLogin(user);
        System.out.println(user);
        if (user1 == null) {
            return ResponseEntity.notFound().build();
        }
        user1.setPassword(null);
        return ResponseEntity.ok(user1);
    }

    /**
     * 普通注册
     *
     * @param user
     */
    @PostMapping("register")
    public ResponseEntity<Boolean> register(@RequestBody User user) {
        System.out.println(user);
        Boolean register = this.userService.register(user);
        if (register == false) {
            return ResponseEntity.accepted().build();
        }
        return ResponseEntity.ok(register);
    }


}
